package com.wlhse.service.impl;

import com.github.pagehelper.PageHelper;
import com.wlhse.dao.MessageDao;
import com.wlhse.dto.getDto.BaseGetDto;
import com.wlhse.dto.inDto.MessageInDto;
import com.wlhse.dto.outDto.MessageDto;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.MessageService;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageDao messageDao;

    @Override
    public String queryMessage(Integer id, BaseGetDto baseGetDto) {
        if (baseGetDto.getType().equals("all")) {
            List<MessageDto> messageDtoList = messageDao.getMessageDtoList(id);
            return NR.r(messageDtoList);
        } else {
            PageHelper.startPage(baseGetDto.getPageIdx(), baseGetDto.getPageSize());
            List<MessageDto> messageDtoList = messageDao.getMessageDtoList(id);
            return NR.r(messageDtoList, messageDao.getMessageDtoListCountNew(id), baseGetDto.getPageIdx());
        }
    }

    @Override
    @Transactional
    public String updateMessageStatus(Integer messageId) {
        if (messageDao.updateMessageStatus(messageId) <= 0)
            throw new WLHSException("更新失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String deleteMessage(Integer messageId) {
        if (messageDao.deleteMessageStatus(messageId) <= 0)
            throw new WLHSException("删除失败");
        return NR.r();
    }

    @Override
    public String queryMessageCount(Integer id) {
        return NR.r(messageDao.getMessageDtoListCount(id));
    }

    @Override
    @Transactional
    public String updateStatusByToUserId(Integer id) {
        if (messageDao.updateStatusByToUserId(id) <= 0)
            throw new WLHSException("更新失败");
        return NR.r();
    }

    @Override
    public String getByStatus(Integer id, MessageInDto messageInDto) {
        if (messageInDto.getType().equals("all")) {
            return NR.r(messageDao.getByStatus(id, messageInDto.getStatus()));
        } else {
            int total = messageDao.getByStatusTotal(id, messageInDto.getStatus());
            PageHelper.startPage(messageInDto.getPageIdx(), messageInDto.getPageSize());
            List<MessageDto> messageDtoList = messageDao.getByStatus(id, messageInDto.getStatus());
            return NR.r(messageDtoList, total, messageInDto.getPageIdx());
        }
    }

}
